﻿//	Copyright (c) 2013 Jonathan Loe
//
//	MIT license (http://en.wikipedia.org/wiki/MIT_License)
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights 
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//	of the Software, and to permit persons to whom the Software is furnished to do so, 
//	subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in all 
//	copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//	FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

using Ordinaire.Db;
using Ordinaire.Commerce.Db;
using Ordinaire.Commerce.Data;

namespace Ordinaire.Commerce
{
    /// <summary>
    /// Represents a class responsible for managing goods data from SQL data source.
    /// </summary>
    public class GoodDataManager : HierarchyDataManager<CContext>, IExist
    {
        #region overrides

        /// <summary>
        /// Returns the preferred DataLoadOptions for current DataManager. This DataLoadOptions will be
        /// used by DataManager.Context when Context is instantiated.
        /// </summary>
        /// <returns>the DataLoadOptions set for current DataManager</returns>
        public override DataLoadOptions GetDataLoadOptions()
        {
            var loadOptions = new DataLoadOptions();
            loadOptions.LoadWith<Goods>(d => d.CompositeGoods);
            return loadOptions;
        }

        #endregion

        /// <summary>
        /// Returns number of matching data with the specified code in the data source.
        /// </summary>
        /// <param name="code">the good code</param>
        /// <returns>the number of matching data</returns>
        public int CountByCode(string code)
        {
            return Context
                .Goods
                .Where(Goods.CodeIs(code))
                .Count();
        }

        /// <summary>
        /// Returns value indicating whether data with the specified id exists in the data source.
        /// </summary>
        /// <param name="id">the id</param>
        /// <returns>true if exists; otherwise false</returns>
        public bool Exists<TId>(TId id)
        {
            return base.Exists<Goods, int, string>((int) (object) id);
        }

        /// <summary>
        /// Returns value indicating where data with specified code exists in the data source.
        /// </summary>
        /// <param name="code">the good code</param>
        /// <returns>true if exists; otherwise false</returns>
        public bool ExistsByCode(string code)
        {
            return (CountByCode(code) > 0) ? true : false;
        }

        /// <summary>
        /// Returns value indicating whether data with the specified name exists in the data source.
        /// </summary>
        /// <param name="name">the good name</param>
        /// <returns>true if exists; otherwise false</returns>
        public bool ExistsByName(string name)
        {
            return base.ExistsByName<Goods, int, string>(name);
        }

        /// <summary>
        /// Returns the data with the specified id from table 'Goods'.
        /// </summary>
        /// <param name="id">the id</param>
        /// <returns>the data with the specified id</returns>
        public Goods Get(int id)
        {
            return base.Get<Goods, int, string>(id);
        }

        /// <summary>
        /// Returns the data with the specified code/barcode from table 'Goods'.
        /// </summary>
        /// <param name="code">the code/barcode</param>
        /// <returns>the good with the specified code/barcode</returns>
        public Goods GetByCode(string code)
        {
            return Context.Goods
                .Where(Goods.CodeIs(code))
                .SingleOrDefault();
        }

        /// <summary>
        /// Returns the data with the specified name from table 'Goods'.
        /// </summary>
        /// <param name="name">the good name</param>
        /// <returns>the good with the specified name</returns>
        public Goods GetByName(string name)
        {
            return GetByName<Goods, int, string>(name);
        }

        /// <summary>
        /// Returns the id of the data with the specified code from table 'Goods'.
        /// </summary>
        /// <param name="code">the good code</param>
        /// <returns>the good id</returns>
        public int GetIdByCode(string code)
        {
            return Context
                .Goods
                .Where(Goods.CodeIs(code))
                .Select(d => d.Id)
                .SingleOrDefault();
        }

        /// <summary>
        /// Returns the data id with the specified name from table 'Goods'.
        /// </summary>
        /// <param name="name">the good name</param>
        /// <returns>the good id</returns>
        public int GetIdByName(string name)
        {
            return base.GetIdByName<Goods, int, string>(name);
        }

        public Goods Insert(string name, string code = null, int unitId = 0, decimal value = (decimal) 0)
        {
            Goods data = new Goods() {
                Name            = name,
                Code            = code,
                MeasureUnitId   = (unitId <= 0) ? (int?) null : unitId, // use null when 0 to avoid from getting constraint error
                Value           = value
            };

            Goods[] results = Insert<Goods, int, string>(data);
            if (results == null || results.Length != 1)
            {
                return null;
            }
            return results[0];
        }
    }
}
